package com.cacho.s2b.lesson.learn;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.net.HttpURLConnection;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

/**
 * @Description  Java 标准库中用来发送 HTTP 请求和接收 HTTP 响应的类
 * @Author LingWei
 * @date 2023/12/31
 **/
public class LearnHttpURLConnection {
    public static void main(String[] args) {
        try {
            // 创建URL对象
            URL url = new URL("https://api.pearktrue.cn/api/lsjt/?type=json");

            // 创建HttpURLConnection对象并设置请求方法为POST
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            // 禁用SSL证书验证（不推荐在生产环境中使用）
            //HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> true);
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            StringBuilder content = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                content.append(line);
                content.append("\n");
            }
            reader.close();
            conn.disconnect();
            System.out.println(content);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}